package com.shangxian.giveithome.controller;

import com.shangxian.giveithome.annoaction.CheckLoginStatus;
import com.shangxian.giveithome.code.CheckCode;
import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.code.ImgFeiCode;
import com.shangxian.giveithome.entity.Img;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.community.IssueImg;
import com.shangxian.giveithome.entity.petCard.PetCardImg;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.ImgMapper;
import com.shangxian.giveithome.mapper.IssueImgMapper;
import com.shangxian.giveithome.mapper.PetCardImgMapper;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.ApiService;
import com.shangxian.giveithome.service.ImgFeiService;
import com.shangxian.giveithome.service.IssueImgService;
import com.shangxian.giveithome.service.PetCardImgService;
import com.shangxian.giveithome.utils.RedisUtils;
import com.shangxian.giveithome.utils.SenInfoCheckUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Api(tags = "文件上传")
@RestController
@RequestMapping("/img")
@Slf4j
public class FastDfsController {

    @Autowired
    private ApiService apiService;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private RedisUtils redisUtils;

    @Resource
    private ImgMapper imgMapper;

    @Autowired
    private ImgFeiService imgFeiService;

    @Resource
    private IssueImgMapper issueImgMapper;

    @Autowired
    private IssueImgService issueImgService;

//    @Autowired
//    private CarouselService carouselService;

    @Autowired
    private PetCardImgService petCardImgService;

    @Resource
    private PetCardImgMapper petCardImgMapper;



    @PostMapping(value = "/upload", name = "上传文件")
    @Transactional
    @CheckLoginStatus
    public Result upload(@RequestParam(value = "id", required = false) Long id, @RequestParam("file") MultipartFile multipartFile) {
        UserDetails header = redisUtils.getHeader(request);
        if (multipartFile == null) {
            ExceptionUtils.error(ImgFeiCode.UPLOAD_NOT_NULL);
        }
        Img inset = imgFeiService.inset(header,id,multipartFile);
        return Result.success(inset);
    }

    @PostMapping(value = "/uploadIssueImg", name = "上传文章文件图片")
    @Transactional
    @CheckLoginStatus
    public Result uploadIssueImg(@RequestParam(value = "id", required = false) Long id, @RequestParam("file") MultipartFile multipartFile) {
        String access_token = apiService.getAccessToken();
        Boolean aBoolean = SenInfoCheckUtil.imgFilter(access_token, multipartFile);
        if (!aBoolean) {
            return Result.error(CheckCode.Img_NOT_Illegal);
        }
        UserDetails header = redisUtils.getHeader(request);
        IssueImg inset = issueImgService.inset(header, id, multipartFile);
        return Result.success(inset);
    }

    /**
     * 上传宠物头像图片信息
     * @param id
     * @param multipartFile
     * @return
     */
    @PostMapping(value = "/uploadPetCardImg", name = "上传宠物头像图片")
    @Transactional
    @CheckLoginStatus
    public Result uploadPetCardImg(@RequestParam(value = "id", required = false) Long id, @RequestParam("file") MultipartFile multipartFile) {
        String access_token = apiService.getAccessToken();
        //调用微信图片校验
        Boolean aBoolean = SenInfoCheckUtil.imgFilter(access_token, multipartFile);
        if (!aBoolean) {
            return Result.error(CheckCode.Img_NOT_Illegal);
        }
        UserDetails header = redisUtils.getHeader(request);
        PetCardImg inset = petCardImgService.inset(header, id, multipartFile);
        return Result.success(inset);
    }

//    @PostMapping(value = "/uploadCarouselImg", name = "上传文章文件图片")
//    @Transactional
//    public Result uploadCarouselImg(@RequestParam("file") MultipartFile multipartFile) {
//        String access_token = apiService.getAccessToken();
//        Boolean aBoolean = SenInfoCheckUtil.imgFilter(access_token, multipartFile);
//        if (!aBoolean) {
//            return Result.error(CheckCode.Img_NOT_Illegal);
//        }
//        Carousel inset = carouselService.insert(multipartFile);
//        return Result.success(inset);
//    }





    @DeleteMapping(value = "/delete", name = "根据id删除图片文件")
    @Transactional
    public Result delete(Long id) {
        UserDetails header = redisUtils.getHeader(request);
        Img img = imgMapper.queryById(id);
        if (img == null) {
            return Result.error(CommonCode.DATA_ERROR);
        }
        return imgFeiService.delete(header, Arrays.asList(img));
    }

    @DeleteMapping(value = "/deleteIssueImg", name = "根据id删除文章图片文件")
    @Transactional
    public Result deleteIssueImg(Long id) {
        UserDetails header = redisUtils.getHeader(request);
        IssueImg issueImg = issueImgMapper.queryById(id);
        if (issueImg == null) {
            return Result.error(CommonCode.DATA_ERROR);
        }
        return issueImgService.delete(header, Arrays.asList(issueImg));
    }

    /**
     * 删除宠物头像图片接口
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deletePetCardImg", name = "根据id删除宠物头像图片文件")
    @Transactional
    public Result deletePetCardImg(Long id) {
        UserDetails header = redisUtils.getHeader(request);
        PetCardImg petCardImg = petCardImgMapper.queryById(id);
        if (petCardImg == null) {
            return Result.error(CommonCode.DATA_ERROR);
        }
        return petCardImgService.delete(header, Arrays.asList(petCardImg));
    }

    @PostMapping("/downloadQrCode")
    @ApiOperation("二维码文件下载")
    public void downloadQrCode(@RequestParam("list") List<Integer> list) throws IOException {
        petCardImgService.downloadQrCode(list);
    }

//    @GetMapping("/downloadQrCode")
//    @ApiOperation("二维码文件下载")
//    public Result downloadQrCodeA(@RequestParam("String")String list){
//        petCardImgService.downloadQrCodeA(list);
//        return Result.success();
//    }

}

